package com.example.projectmanagement.controller;

import com.example.projectmanagement.aop.LogOperation;
import com.example.projectmanagement.model.Role;
import com.example.projectmanagement.model.Permission;
import com.example.projectmanagement.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/roles")
@CrossOrigin
public class RoleController {
    
    @Autowired
    private RoleService roleService;
    
    /**
     * 获取所有角色
     */
    @GetMapping
    public ResponseEntity<List<Role>> getAllRoles() {
        List<Role> roles = roleService.getAllRoles();
        return ResponseEntity.ok(roles);
    }
    
    /**
     * 根据ID获取角色
     */
    @GetMapping("/{id}")
    public ResponseEntity<Role> getRoleById(@PathVariable Long id) {
        Role role = roleService.getRoleById(id);
        return ResponseEntity.ok(role);
    }
    
    /**
     * 创建角色
     */
    @PostMapping
    @LogOperation(operationType = "CREATE", module = "角色管理", description = "创建新角色")
    public ResponseEntity<Role> createRole(@RequestBody Role role) {
        // 检查是否尝试创建ADMIN角色
        if ("ADMIN".equals(role.getRoleName())) {
            throw new RuntimeException("不能创建新的系统管理员角色");
        }
        
        Role createdRole = roleService.createRole(role);
        return ResponseEntity.ok(createdRole);
    }
    
    /**
     * 更新角色
     */
    @PutMapping("/{id}")
    @LogOperation(operationType = "UPDATE", module = "角色管理", description = "更新角色信息")
    public ResponseEntity<Role> updateRole(@PathVariable Long id, @RequestBody Role roleDetails) {
        // 检查是否尝试将角色重命名为ADMIN
        if ("ADMIN".equals(roleDetails.getRoleName())) {
            // 获取角色当前名称
            Role currentRole = roleService.getRoleById(id);
            if (!"ADMIN".equals(currentRole.getRoleName())) {
                throw new RuntimeException("不能将角色重命名为系统管理员");
            }
        }
        
        Role updatedRole = roleService.updateRole(id, roleDetails);
        return ResponseEntity.ok(updatedRole);
    }
    
    /**
     * 删除角色
     */
    @DeleteMapping("/{id}")
    @LogOperation(operationType = "DELETE", module = "角色管理", description = "删除角色")
    public ResponseEntity<Void> deleteRole(@PathVariable Long id) {
        // 检查是否尝试删除ADMIN角色
        Role role = roleService.getRoleById(id);
        if ("ADMIN".equals(role.getRoleName())) {
            throw new RuntimeException("不能删除系统管理员角色");
        }
        
        roleService.deleteRole(id);
        return ResponseEntity.ok().build();
    }
    
    /**
     * 获取所有权限
     */
    @GetMapping("/permissions")
    public ResponseEntity<List<Permission>> getAllPermissions() {
        List<Permission> permissions = roleService.getAllPermissions();
        return ResponseEntity.ok(permissions);
    }
    
    /**
     * 根据角色ID获取权限
     */
    @GetMapping("/{id}/permissions")
    public ResponseEntity<List<Permission>> getPermissionsByRoleId(@PathVariable Long id) {
        List<Permission> permissions = roleService.getPermissionsByRoleId(id);
        return ResponseEntity.ok(permissions);
    }
    
    /**
     * 为角色分配权限
     */
    @PostMapping("/{id}/permissions")
    @LogOperation(operationType = "UPDATE", module = "角色管理", description = "为角色分配权限")
    public ResponseEntity<Void> assignPermissionsToRole(@PathVariable Long id, @RequestBody List<Long> permissionIds) {
        roleService.assignPermissionsToRole(id, permissionIds);
        return ResponseEntity.ok().build();
    }
    
    /**
     * 从角色中移除权限
     */
    @DeleteMapping("/{id}/permissions/{permissionId}")
    @LogOperation(operationType = "DELETE", module = "角色管理", description = "从角色中移除权限")
    public ResponseEntity<Void> removePermissionFromRole(@PathVariable Long id, @PathVariable Long permissionId) {
        roleService.removePermissionFromRole(id, permissionId);
        return ResponseEntity.ok().build();
    }
}